package com.qianyucc.blog.model.vo;

import cn.hutool.core.bean.*;
import com.qianyucc.blog.model.entity.*;
import com.qianyucc.blog.utils.*;
import lombok.*;

import java.io.*;
import java.util.*;

import static com.qianyucc.blog.global.Constants.DEFAULT_DATE_TIME_PATTERN;

/**
 * @author lijing
 * @date 2019-10-12 14:29
 * @description 封装向前端返回的简化版的文章信息
 */
@Data
public class SimpleArticleVO implements Serializable {
    private static final long serialVersionUID = 5049547324456986837L;

    public static SimpleArticleVO create(ArticleDO articleDO){
        SimpleArticleVO simpleArticleVO = new SimpleArticleVO();
        BeanUtil.copyProperties(articleDO, simpleArticleVO);
        // 防止tags为空
        simpleArticleVO.setTags(Optional.ofNullable(articleDO.getTags())
                .map(tags -> tags.split(","))
                .orElse(new String[0]));
        simpleArticleVO.setGmtUpdate(Utils.formatDate(articleDO.getGmtUpdate(), DEFAULT_DATE_TIME_PATTERN));
        return simpleArticleVO;
    }

    private String id;
    private String author;
    private String title;
    private String[] tags;
    private Integer type;
    private String category;
    private String gmtUpdate;
    private String tabloid;
    private Integer comments;
    private Integer views;
}
